package com.yuda.campsite.service.impl;

import com.yuda.campsite.domain.InvitationCode;
import com.yuda.campsite.domain.InvitationCodeClassification;
import com.yuda.campsite.dto.*;
import com.yuda.campsite.mapper.InvitationCodeClassificationMapper;
import com.yuda.campsite.mapper.InvitationCodeMapper;
import com.yuda.campsite.service.IInvitationCodeService;
import com.yuda.common.core.utils.DateUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

/**
 * 邀请码Service业务层处理
 *
 * @author wyn
 * @date 2021-09-29
 */
@Service
public class InvitationCodeServiceImpl implements IInvitationCodeService {

    @Autowired
    private InvitationCodeMapper invitationCodeMapper;

    @Autowired
    private InvitationCodeClassificationMapper invitationCodeClassificationMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询邀请码列表
     *
     * @param invitationCode 邀请码
     * @return 邀请码
     */
    @Override
    public List<InvitationWithStuInfo> selectInvitationCodeList(InvitationWithStuInfo invitationCode) {
        return invitationCodeMapper.selectInvitationCodeList(invitationCode);
    }

    /**
     * 新增邀请码
     *
     * @return 结果
     */
    @Transactional
    @Override
    public int insertInvitationCode(SimpleInvCodeForGen simpleInvCodeForGen) {

        InvitationCodeClassification invitationCodeClassification = populateBeanClassification(simpleInvCodeForGen);
        invitationCodeClassificationMapper.insertInvitationCodeClassification(invitationCodeClassification);
        assert invitationCodeClassification.getId() != null : "保存邀请码类型失败";
        int classificationId = invitationCodeClassification.getId();
        //准备邀请码,封装对象准备 批量入库
        ArrayList<InvitationCode> objects = Lists.newArrayList();
        LocalDateTime now = LocalDateTime.now();
        String year = String.valueOf(now.getYear()).substring(2);

        int finalClassificationId = classificationId;
        for (Integer i = 0; i < simpleInvCodeForGen.getNum(); i++) {
            String invitationCodeYd = stringRedisTemplate.opsForList().leftPop("invitationCodeYd");
            InvitationCode invitationCode = populateBeanInvitationCode(simpleInvCodeForGen, invitationCodeYd);
            invitationCode.setClassifictionId(finalClassificationId);
            invitationCode.setCode("CAMP-" + year + String.format("%02d", now.getMonthValue()) + "-" + invitationCodeYd);
            objects.add(invitationCode);
        }
        invitationCodeMapper.batchInsertInvitationCode(objects);
        return 1;
    }

    @Override
    public List<String> name(Integer campsiteId) {
        return invitationCodeMapper.campNames(campsiteId);
    }

    @Override
    public int invCodeNameisUsed(SimpleInvCodeForGen simpleInvCodeForGen) {
        return invitationCodeMapper.invCodeNameisUsed(simpleInvCodeForGen);
    }

    @Override
    public int appendInvitationCode(SimpleInvCodeForGen simpleInvCodeForGen) {

        //获取classificationId
        int classificationId = invitationCodeMapper.getClassificationId(simpleInvCodeForGen.getName(), simpleInvCodeForGen.getCampsiteId());
        //准备邀请码,封装对象准备 批量入库
        ArrayList<InvitationCode> objects = Lists.newArrayList();
        LocalDateTime now = LocalDateTime.now();
        String year = String.valueOf(now.getYear()).substring(2);
        for (int i = 0; i < simpleInvCodeForGen.getNum(); i++) {
            String invitationCodeYd = stringRedisTemplate.opsForList().leftPop("invitationCodeYd");
            InvitationCode invitationCode = populateBeanInvitationCode(simpleInvCodeForGen, invitationCodeYd);
            invitationCode.setClassifictionId(classificationId);
            invitationCode.setCode("CAMP-" + year + String.format("%02d", now.getMonthValue()) + "-" + invitationCodeYd);
            objects.add(invitationCode);
        }
        invitationCodeMapper.batchInsertInvitationCode(objects);
        return 1;
    }

    @Override
    public List<InvitationCategory> categoryList(Integer campsiteId) {
        return invitationCodeMapper.categoryList(campsiteId);
    }

    @Override
    public List<InvitationCategoryDetail> categoryDetailList(Integer campsiteId, String reduction) {
        return invitationCodeMapper.categoryDetailList(campsiteId, reduction);
    }

    @Override
    public List<InvCodeWithStuInfo> getInvCodeWithStuInfo(Integer campsiteId, String reduction, String name) {
        return invitationCodeMapper.getInvCodeWithStuInfo(campsiteId, reduction, name);
    }

    private InvitationCode populateBeanInvitationCode(SimpleInvCodeForGen simpleInvCodeForGen, String invitationCodeYd) {
        InvitationCode invitationCode = new InvitationCode();
        invitationCode.setCampsiteId(simpleInvCodeForGen.getCampsiteId());
        invitationCode.setCreateTime(DateUtils.getNowDate());
        invitationCode.setReduction(simpleInvCodeForGen.getReduction());
        return invitationCode;
    }

    private InvitationCodeClassification populateBeanClassification(SimpleInvCodeForGen simpleInvCodeForGen) {

        InvitationCodeClassification invitationCodeClassification = new InvitationCodeClassification();
        invitationCodeClassification.setCampsiteId(simpleInvCodeForGen.getCampsiteId());
        invitationCodeClassification.setName(simpleInvCodeForGen.getName());
        invitationCodeClassification.setReduction(simpleInvCodeForGen.getReduction());
        invitationCodeClassification.setCreateTime(DateUtils.getNowDate());

        return invitationCodeClassification;
    }

}
